package com.thin.tomcat;

import java.io.IOException;
import java.io.InputStream;

/**
 * 对http协议进行解析获取请求地址和方法以及参数
 * 
 * @author zincredible
 * @date 2019/05/30 14:06:33
 */
public class Request {

	private String method;
	private String url;

	public Request(InputStream inputStream) throws IOException {
		String httpRequest = "";
		byte[] content = new byte[1024];
		int len = 0;
		if ((len = inputStream.read(content)) > 0) {
			httpRequest = new String(content, 0, len);
		}
		if (len < 0) {
			return;
		}
		System.out.println("1=>" + httpRequest + "|" + len);
		String httpHead = httpRequest.split("\n")[0];
		System.out.println("2=>" + httpHead);
		String tmpUrl = httpHead.split("\\s")[1];
		if (tmpUrl.indexOf("?") != -1) {
			url = tmpUrl.substring(0, tmpUrl.indexOf("?"));
		} else {
			url = tmpUrl;
		}

		method = httpHead.split("\\s")[0];
		System.out.println(this);
		System.out.println("+++++++++++++++++++++++++++++");
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Request [method=" + method + ", url=" + url + "]";
	}

}
